package com.yihaodian.index.meta;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

public class IndexMetas {

	// IndexId -> IndexMeta
	ImmutableMap<String, IndexMeta> _metaMap = null;

	public IndexMetas(ImmutableMap<String, IndexMeta> metaMap) {
		this._metaMap = metaMap;
	}

	public IndexMeta indexMeta(String index) {
		return _metaMap.get(index);
	}

	public Set<String> indexNames() {
		return _metaMap.keySet();
	}

	public static class Builder {
		Map<String, IndexMeta> maps;

		public Builder() {
			maps = Maps.newHashMap();
		}

		public Builder(IndexMetas metas) {
			if (metas._metaMap == null) {
				maps = Maps.newHashMap();
			} else {
				maps = new HashMap<String, IndexMeta>(metas._metaMap);
			}
		}

		public Builder add(IndexMeta indexMeta) {
			maps.put(indexMeta.getIndexId(), indexMeta);

			return this;
		}

		public Builder remove(String index) {
			maps.remove(index);

			return this;
		}

		public IndexMetas build() {
			return new IndexMetas(new ImmutableMap.Builder<String, IndexMeta>()
					.putAll(maps).build());
		}
	}

}
